#ifndef _MS_VOXEL_IMAGE_H_

#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <iostream>

class VoxelImage;

class VoxelImage {
	public:
		unsigned char *** data;
		int Nx, Ny, Nz;
		double h;
		double wx, wy, wz;
		double length;

	public:
		VoxelImage( const double L = 1., const int N = 11, const unsigned char col = 255 );
		VoxelImage( const double Lx, const double Ly, const double Lz, const double H, const unsigned char col = 255 );
		~VoxelImage();

		void GetVoxelCoordinates( const double x, const double y, const double z, int & ix, int & iy, int & iz ) const;
		inline int GetVoxelCoX( const double x ) const
				{	return (int( (x + 0.5*wx )/h ));	}
// 				{	return (int(x/h + 0.5) + (Nx-1)/2);	}
		inline int GetVoxelCoY( const double y ) const
				{	return (int( (y + 0.5*wy )/h ));	}
// 				{	return (int(y/h + 0.5) + (Ny-1)/2);	}
		inline int GetVoxelCoZ( const double z ) const
				{	return (int( (z + 0.5*wz )/h ));	}
// 				{	return (int(z/h + 0.5) + (Nz-1)/2);	}
	
		inline double GetX( const int ix ) const { return ( -0.5*wx+h*(0.5+(double)ix) ); }
		inline double GetY( const int iy ) const { return ( -0.5*wy+h*(0.5+(double)iy) ); }
		inline double GetZ( const int iz ) const { return ( -0.5*wz+h*(0.5+(double)iz) ); }
		
		void SetData(int x, int y, int z);
		
		
		

		inline bool	XAdmissible( const double x ) const {
			const int ix=GetVoxelCoX(x);
			return ( (ix >= 0) && (ix < Nx) );
		}
		inline bool	YAdmissible( const double y ) const {
			const int iy=GetVoxelCoY(y);
			return ( (iy >= 0) && (iy < Ny) );
		}
		inline bool	ZAdmissible( const double z ) const {
			const int iz=GetVoxelCoZ(z);
			return ( (iz >= 0) && (iz < Nz) );
		}
		inline bool	XAdmissible( const int ix ) const {
			return ( (ix >= 0) && (ix < Nx) );
		}
		inline bool	YAdmissible( const int iy ) const {
			return ( (iy >= 0) && (iy < Ny) );
		}
		inline bool	ZAdmissible( const int iz ) const {
			return ( (iz >= 0) && (iz < Nz) );
		}
		void	Erase( const unsigned char bgrd_col = 255 );
		void	DrawBox( const double x0[3], const double x1[3], const unsigned char col = 0 );
		int		DrawCircleZ( const double x[0], const double r, const unsigned char col = 0 );
		void	DrawLine(const int x0[], const int x1[], const unsigned short col = 0 );
		void	DrawLine(const double X0[], const double X1[], const unsigned short col = 0);

		void	WriteImageStack( const char fn[] ) const ;
		void	WriteVTKFile( const char fn[] ) const ;
		int 	ShowNumber( char fn[] );
};

#define _MS_VOXEL_IMAGE_H_
#endif /* _MS_VOXEL_IMAGE_H_ */

